package com.atech.ebayadmin.common.Intercepotr;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.atech.ebayadmin.system.vo.UserVO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class TimeoutIntercepter extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log = LogManager.getLogger(TimeoutIntercepter.class);
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		String actionName = invocation.getInvocationContext().getName();
		log.info("Action Name is: " + actionName);
		if (StringUtils.equalsIgnoreCase("userLogin", actionName)) {
			//return null;
			return invocation.invoke();
		}
		if (StringUtils.equalsIgnoreCase("loginOut", actionName)) {
			//return null;
			return invocation.invoke();
		}
		ActionContext ctx = invocation.getInvocationContext();
		boolean timeout = false;
		if (null == ctx.getSession()) {
			log.info("session is null");
			timeout = true;
		} else {
			UserVO userVo = (UserVO) ctx.getSession().get("User");
			if (null == userVo) {
				log.info("user is null");
				timeout = true;
			}
		}
		log.info("The timeout interceptor result is: " + timeout);
		if (timeout) {
			return "timeout";
		} else {
			return invocation.invoke();
			//return null;		
		}
	}

}
